FUZZER_NAME := 'fuzzbench'
PROJECT_DIR := absolute_path(".")
PROFILE := env("PROFILE", "release")
PROFILE_DIR := if PROFILE == "release" { "release" } else if PROFILE == "dev" { "debug" } else { "debug" }
CARGO_TARGET_DIR := env("CARGO_TARGET_DIR", "target")
FUZZER := CARGO_TARGET_DIR / PROFILE_DIR / FUZZER_NAME


alias build := fuzzer

alias cc := cxx

[linux]
[macos]
cxx:
	cargo build --profile={{PROFILE}}

[windows]
cxx:
	echo "Unsupported on this platform"

[linux]
[macos]
fuzz_o: cxx
	{{CARGO_TARGET_DIR}}/{{PROFILE_DIR}}/libafl_cc --libafl-no-link -O3 -g -c fuzz.c -o fuzz.o

[windows]
fuzz_o:
	echo "Unsupported on this platform"

[linux]
[macos]
fuzzer: cxx fuzz_o
	{{CARGO_TARGET_DIR}}/{{PROFILE_DIR}}/libafl_cxx --libafl fuzz.o -o {{FUZZER_NAME}} -lm -lz

[windows]
fuzzer:
	echo "Unsupported on this platform"
	

[linux]
[macos]
run: cxx fuzz_o
	#!/bin/bash
	rm -rf libafl_unix_shmem_server || true
	mkdir in || true
	echo a > in/a
	./{{FUZZER_NAME}} -o out -i in

[windows]
run:
	echo "Unsupported on this platform"

[linux]
[macos]
test: fuzzer test-dump-cov
	#!/bin/bash
	rm -rf libafl_unix_shmem_server || true
	mkdir in || true
	echo a > in/a
	# Allow sigterm as exit code 
	timeout 31s ./{{FUZZER_NAME}} -o out -i in | tee fuzz_stdout.log || true
	if grep -qa "objectives: 1" fuzz_stdout.log; then
		echo "Fuzzer is working"
	else
		echo "Fuzzer does not generate any testcases or any crashes"
		exit 1
	fi
	rm -rf out || true
	rm -rf in || true

[windows]
test: fuzzer
	echo "Unsupported on this platform"

clean:
	cargo clean

test-dump-cov:
	cargo build --profile={{PROFILE}} --features dump_cov
	{{CARGO_TARGET_DIR}}/{{PROFILE_DIR}}/libafl_cc --libafl-no-link -O3 -g -c fuzz.c -o fuzz.o
	{{CARGO_TARGET_DIR}}/{{PROFILE_DIR}}/libafl_cxx -g -rdynamic --libafl fuzz.o -o {{FUZZER_NAME}} -lm -lz
	# Create a dummy seed
	mkdir -p seeds_dump
	mkdir -p coverage_dump
	echo "dummy" > seeds_dump/dummy
	# Run with --dump-cov
	./fuzzbench -i seeds_dump -o corpus_dump --dump-cov coverage_dump seeds_dump/dummy
	# Verify output
	ls -l coverage_dump/dummy.info
	grep "TN:" coverage_dump/dummy.info
	grep "TN:" coverage_dump/dummy.info
	if command -v genhtml >/dev/null 2>&1; then \
		genhtml coverage_dump/dummy.info --output-directory coverage_dump/html; \
		ls coverage_dump/html/index.html; \
	else \
		echo "genhtml not found, skipping HTML report generation"; \
	fi
	# Clean up
	rm -rf seeds_dump corpus_dump coverage_dump
